package l1j.server.server.datatables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.utils.SQLUtil;

public class CharacterInfoTable {
	private static Logger _log = Logger.getLogger(CharacterInfoTable.class.getName());
	private static CharacterInfoTable _instance;
	
	public static CharacterInfoTable getInstance() {
		if (_instance == null) {
			_instance = new CharacterInfoTable();
		}
		return _instance;
	}

	
	public void getvalue(L1PcInstance pc,String key){
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {

			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("select * from character_info where id=?");

					//"INSERT INTO log_chat (account_name, char_id, name, clan_id, clan_name, locx, locy, mapid, type, target_account_name, target_id, target_name, target_clan_id, target_clan_name, target_locx, target_locy, target_mapid, content, datetime) 
					//VALUE (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, SYSDATE())");
			
			
			pstm.setInt(1, pc.getId());
			//rs = pstm.execute();
			rs = pstm.executeQuery();
			while(rs.next()){
				pc.sendPackets(new S_SystemMessage(rs.getString(key)));
			}

		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}	
	}

	public void commit(int mode) {
		/* mode 1 : day
		 * mode 2 : week
		 */

		Connection con = null;
		PreparedStatement pstm = null;
		String q;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			
			if(mode == 1) {
				q="UPDATE character_info SET "
					+"boss=boss+boss_today, "
					+"boss_day=boss_today, "
					//+"boss_week=boss_today + boss_week, "
					+"boss_today=0";
			} else {
				q="UPDATE character_info SET "
					+"boss_day=boss_today, "
					+"boss_week=boss_today + boss, "
					+"boss=0, "
					+"boss_today=0";
			}
			pstm = con.prepareStatement(q);
			pstm.execute();

		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}	
		  
		
	}
	
	public void updateMob(int id) {
		update(id, "boss_today",1);
		
	}
	
	public void update(int id,String key,int value){
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("update character_info "
							+ "set " + key + " = " + key + " + ? where id=?"
					);
					//"INSERT INTO log_chat (account_name, char_id, name, clan_id, clan_name, locx, locy, mapid, type, target_account_name, target_id, target_name, target_clan_id, target_clan_name, target_locx, target_locy, target_mapid, content, datetime) 
					//VALUE (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, SYSDATE())");
			
			pstm.setInt(1, value);
			pstm.setInt(2, id);
			pstm.execute();

		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}	
	}

	
	public void create(L1PcInstance pc){
		create(pc.getId(), pc.getExp());
	}
	
	public void create(int id, int exp){
		Connection con = null;
		PreparedStatement pstm = null;
		try {

			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("INSERT INTO character_info"
							+"  (id, create_date, exp)"
							+"VALUES"
							+"  (?, SYSDATE(), ?)"
					);

					//"INSERT INTO log_chat (account_name, char_id, name, clan_id, clan_name, locx, locy, mapid, type, target_account_name, target_id, target_name, target_clan_id, target_clan_name, target_locx, target_locy, target_mapid, content, datetime) 
					//VALUE (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, SYSDATE())");
			
			pstm.setInt(1, id);
			pstm.setInt(2, exp);
			pstm.execute();
			
			

		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}	
	}


	public int getBestValue(String value) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		int result = 0;
		try {

			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("select id from character_info order by ? desc limit 0,1 ");
			
			pstm.setString(1, value);
			rs = pstm.executeQuery();
			while(rs.next()){
				result = rs.getInt(value);
			}

		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}	
		return result;
	}

	public int getBossHunter() {
		return getBestValue("boss_day");
	}

	public int getBossMaster() {
		return getBestValue("boss_week");
	}
}
